class EgretVideo extends eui.Group {
    private video: egret.Video
    public loaded: boolean = false
    public autoPlay: boolean = false

    public constructor(url: string, poster: string = "", width: number = 640, height: number = 320) {
        super()
        this.video = new egret.Video()
        this.video.x = 0 //设置视频坐标x
        this.video.y = 0 //设置视频坐标y
        this.video.width = width //设置视频宽
        this.video.height = height //设置视频高
        this.video.fullscreen = false //设置是否全屏（暂不支持移动设备）
        this.video.poster = poster //设置loding图
        if (url.length > 0) {
            this.load(url, null)
        }
        this.addChild(this.video) //将视频添加到舞台
    }
    private onLoadErr() {
        console.log("video load error happened")
    }
    public play(startTime?: number, loop?: boolean) {
        if (this.loaded) {
            this.video.play(startTime, loop)
        }
    }
    public pause() {
        if (this.loaded) {
            this.video.pause()
        }
    }

    public load(url: string, callback: () => {}) {
        this.loaded = false
        this.video["waiting"] = true
        this.video.load(url, true)
        if (this.video["video"]) {
            this.video["video"].muted = true
        }
        this.video.once(egret.Event.COMPLETE, () => {
            this.loaded = true
            if (callback) {
                callback()
            }
            this.dispatchEventWith("loaded")
            if (this.autoPlay) {
                this.play()
            }
        }, this)
        this.video.once(egret.IOErrorEvent.IO_ERROR, this.onLoadErr, this)
    }
    public get bitmapData(): egret.BitmapData {
        return this.video.bitmapData
    }
}
